package org.example.file;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Stream;

public class FileReadExample {
    public static final String FILE_PATH = "C:\\Users\\JohnConnor\\Documents\\Downloads\\catalina.out";
    public static void main(String[] args) throws IOException {
        test8();
    }

    static void test1() throws IOException {
        Stream<String> lines = Files.lines(Paths.get(FILE_PATH));
        lines.filter(line -> line.contains("INSERT INTO"))
                .forEach(System.err::println);
    }

    static void test2() throws IOException {
        Stream<String> lines = Files.lines(Paths.get(FILE_PATH));
        lines.parallel().forEach(System.err::println);
        System.err.println(String.join("", Collections.nCopies(50, "-")));

    }

    static void test3() throws IOException {
        byte[] bytes = Files.readAllBytes(Paths.get(FILE_PATH));
        String content = new String(bytes, StandardCharsets.UTF_8);
        System.err.println(content);
    }

    static void test4() throws IOException {
        List<String> lines = Files.readAllLines(Paths.get(FILE_PATH),StandardCharsets.UTF_8);
        lines.forEach(System.err::println);
    }

    static void test5() throws FileNotFoundException {
        try (BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH))) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.err.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static void test6() throws IOException {
        int bufferSize = 10240;
        try (BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH), bufferSize)) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.err.println(line);
            }
        }
    }

    static void test7() throws IOException {
        try (BufferedReader reader = Files.newBufferedReader(Paths.get(FILE_PATH))) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.err.println(line);
            }
        }
    }

    static void test8() throws FileNotFoundException {
        try (Scanner scanner = new Scanner(new FileReader(FILE_PATH))) {
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                System.err.println(line);
            }
        }
    }
}
